package com.qst.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qst.domain.VipExpensesRecord;
import com.qst.dto.Result;
import com.qst.mapper.VipExpensesRecordMapper;
import com.qst.service.IVipExpensesRecordService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.qst.util.SystemConstants.EXCHANGE_NAME;

@Service
public class VipExpensesRecordImpl extends ServiceImpl<VipExpensesRecordMapper, VipExpensesRecord> implements IVipExpensesRecordService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    @Transactional
    public Result saveExpensesRecord(VipExpensesRecord vipExpensesRecord) {
        // 更新数据库
        boolean success = save(vipExpensesRecord);
        if (!success) {
            return Result.fail("流水录入失败！");
        }

        rabbitTemplate.convertAndSend(EXCHANGE_NAME,"", vipExpensesRecord);
        return Result.ok("流水录入成功！");
    }

    @Override
    @Transactional
    public Result bookkeeping(VipExpensesRecord vipExpensesRecord) {
        Long expensesId = vipExpensesRecord.getExpensesId();
        if (expensesId == null) {
            return Result.fail("流水id不能为空！");
        }
        // 1.更新数据库
        boolean isSuccess = update().setSql("bookkeeping = 1").eq("expenses_id", expensesId).update();
        if (!isSuccess){
            return Result.ok("更新失败！");
        }
        return Result.ok("更新成功！");
    }
}
